package palidino76.rs2.content.skills;

import palidino76.rs2.util.Misc;
import palidino76.rs2.player.Player;
import palidino76.rs2.Engine;
import palidino76.rs2.player.items.PlayerItems;

public class Mining {

    Player p;

    /**
     * Class constructor.
     * @param player the player which the constructor should be made for.
     */

    public Mining(Player p) {
        this.p = p;
    }

    /**
     * Mining system.
      */

    public void createMiningEvent() {
        if (p == null) {
            resetMiningEvent();
            return;
        }
        if (getPick() == -1) {
            p.frames.sendMessage(p, "You don't have a pickaxe");
            return;
        }
        if (getlevelpick(getPick()) < p.getLevelForXP(14) == false) {
            p.frames.sendMessage(p, "You need a level of " + getlevelpick(getPick()) + " to use the pickaxe");
            resetMiningEvent();
            return;
        }
        if (getLevelForOre() < p.getLevelForXP(14) == false) {
            p.frames.sendMessage(p, "You need a level of " + getLevelForOre() + " to mine this");
            resetMiningEvent();
            return;
        }
        p.requestAnim(getAnimation(), 0);
        setOreDelay(selectRandomDelay());
        p.frames.sendMessage(p, "You swing your pick at the rock..");
    }

    /**
     * If player's ore delay reached zero, The player receives an ore and anim resets. Mining resets to.
     */

    public void receiveOre() {
        if (p == null || !playerIsMining()) {
            resetMiningEvent();
            return;
        }
        if (getOreDelay() == 0) {
            p.requestAnim(-1, 0);
            Engine.playerItems.addItem(p, getOreType(), 1);
            p.addSkillXP(getXP(), 14);
            p.frames.sendMessage(p, "You succes fully mined an " + Engine.items.getItemName(getOreType()));
            resetMiningEvent();
        }
    }

    /**
     * Get the requested animation for the pickaxe.
     * @return the pickaxe's animation id.
     */

        public boolean hasItem(int id) {
        PlayerItems pi = new PlayerItems();
        return pi.invItemCount(p, id) > 0;
        }

        int getPick() {
        if (hasItem(1265)) return 1265;
        if (hasItem(1275)) return 1275;
        if (p.equipment[3] == 1265) return 1265;
        if (p.equipment[3] == 1275) return 1275;
        return -1;
        }

    int getAnimation() {
        switch (getPick()) {

            case 1265: //Bronze Pickaxe
                return 625;
            case 1275: // Rune Pickaxe
                return 624;
            default:
                return -1;
        }
    }

    /**
     * Return the rock type.
     * @return the rock type to receive the correct ore.
     */

        int getOreType() {
        switch (p.oreType) {

                        case 1: //Tin
                return 438;
                        case 2: //Copper
                return 436;
            case 3: //Iron
                                return 440;
                        case 4: //Silver
                return 442;
            case 5: //Coal
                    return 453;
                        case 6: //Gold
                return 444;
                        case 7: //Mithrill
                return 447;
                        case 8: //Adamantite
                                return 449;
                        case 9: //Runite
                                return 451;
            default:
                return -1;
        }
    }

    /**
     * Return the Xp gained of rock type.
     * @return the right amount of XP.
     */

    int getXP() {
        switch(p.oreType) {
    
            case 1: //Tin
                return 300;
            case 2: //Copper
                return 500;
            case 3: //Iron
                return 750;
            case 4: //Silver
                return 1150;
            case 5: //Coal
                return 2100;
            case 6: //Gold
                return 4350;
            case 7: //Mithrill
                return 6550;
            case 8: //Adamantite
                return 7750;
            case 9: //Runite
                return 8900;
            default:
                return -1;
        }
    }

    /**
     * Return the level required to mine.
     * @return the right level.
     */

    int getLevelForOre() {

        switch(p.oreType) {

            case 1: //Tin
                return 1;
            case 2: //Copper
                return 1;
            case 3: //Iron
                return 15;
            case 4: //Silver
                return 20;
            case 5: //Coal
                return 30;
            case 6: //Gold
                return 40;
            case 7: //Mithrill
                return 55;
            case 8: //Adamantite
                return 70;
            case 9: //Runite
                return 85;
            default:
                return -1;
        }
    }

    /**
     * Return the level required to use the pickaxe with.
     * @return the right level.
     */

    int getlevelpick(int pick) {
        switch (pick) {

            case 1265:
                return 1;
            case 1275:
                return 41;
        }
        return -1;
    }

    /**
     * Get the player's ore delay.
     * @return the ore delay.
     */

     int getOreDelay() {
        return p.oreDelay;
    }


    /**
     * If player is mining an rock.
     * @return if player is mining.
     */

    boolean playerIsMining() {
        return p.isMining;
    }

    /**
     * Select a random delay
     * @return the delay thats going to be setted for the ore delay.
     */

    int selectRandomDelay() {
        int highestDelay = 14, lowestDelay = 6;
        int delay = Misc.random(14);
        int mining = p.skillLvl[14];
        if (mining >= delay && delay <= highestDelay && delay >= lowestDelay)
            return delay;
        else
            return delay - lowestDelay + 4;
    }

    /**
     * Set player mining.
     * @param isMining if player is mining.
     */

    void setMining(boolean isMining) {
        p.isMining = isMining;
    }

    /**
     * Set ore delay.
     * @param oreDelay the delay thats going to be setted.
     */

    void setOreDelay(int oreDelay) {
        p.oreDelay = oreDelay;
    }

    /**
     * Set the player's rock type to receive after mining the correct ore.
     * @param oreType the rock type of the ore thats mined later etc.
     */

    void setOreType(int oreType) {
        p.oreType = oreType;
    }

    /**
     * Reset the mining process.
     */
    public void resetMiningEvent() {
        setOreType(0);
        setMining(false);
        setOreDelay(-1);
    }

}